package com.ls.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.user.model.*;
import com.ls.entity.Customer;
import com.ls.entity.SysUser;
import com.ls.service.ISysUserService;
import com.ls.utils.ResultData;
import com.ls.utils.login.CurrentUser;
import com.ls.utils.rsa.RsaUtils;
import com.ls.utils.weixin.WxUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author pengyu
 * @since 2020-05-21
 */
@Api(value = "用户相关接口", tags = "用户相关接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {
    private final ISysUserService userService;
    /**用户手机号获取验证码  type 验证码类型 0 本号码 1 密保手机 getSmsVerifyCode phone */
    @GetMapping(value = "getSmsVerifyCode")
    public ResultData getSmsVerifyCode(@RequestParam  String phone ,@RequestParam  int type) throws Exception {
        return userService.getSmsVerifyCode(phone ,type);
    }

    /** 重置密码 */
    @PostMapping(value = "reSetPwd")
    public ResultData reSetPwd(@Valid @RequestBody ReSetPwdModel model) {
        return userService.reSetPwd(model);
    }

    /** 修改密码 */
    @PostMapping(value = "updatePwd")
    public ResultData updatePwd(@Valid @RequestBody UpdatePwdModel model) {
        return userService.updatePwd(model);
    }

    /** 查询用户列表 超级管理员 客服 省份管理员 代理商 */
    @GetMapping(value = "getPaging")
    public ResultData getPaging(QueryUserModel model) {
        return userService.getPaging(model);
    }

    /** 用户有效状态变更 */
    @PostMapping(value = "updateStatus")
    public ResultData updateStatus(@Valid @RequestBody UpdateStatusModel model) {
        return userService.updateStatus(model);
    }

    /** 新增用户 */
    @PostMapping(value = "add")
    public ResultData add(@Valid @RequestBody AddUserModel model) {
        return userService.add(model);
    }

    /** 修改用户 */
    @PostMapping(value = "update")
    public ResultData update(@Valid @RequestBody UpdateUserModel model) {
        return userService.update(model);
    }

    /** 通过用户id 查询用户信息的接口 数据库同步操作 */
    @GetMapping(value = "getById")
    public ResultData getById(@RequestParam Long userId) {
        return userService.getByUserId(userId);
    }
    /**
     * 通过网页的code查询openid返回给前端
     */
    @GetMapping(value = "getWebOpenIdByCode")
    public ResultData getWebOpenIdByCode(@RequestParam  String code
            , @RequestParam String appId
            , @RequestParam String appSecret) throws Exception {
        JSONObject jsonObject = WxUtil.getOpenIdAndAccessToken(appId, appSecret, code);
        String openId = jsonObject.getString("openid");
        if (StringUtils.isEmpty(openId)) {
            return ResultData.failure("登录失败code错误");
        } else {
            return ResultData.successData(openId);
        }
    }

}

